package com.yy.controller.admin;

import com.yy.dto.SetmealDTO;
import com.yy.dto.SetmealPageQueryDTO;
import com.yy.result.PageResult;
import com.yy.result.Result;
import com.yy.service.SetmealService;
import com.yy.vo.SetmealVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealDTO dto) {
        setmealService.updateSetmeal(dto);
        return Result.success();
    }

    @GetMapping("/page")
    public Result page(SetmealPageQueryDTO dto) {
        PageResult pageResult = setmealService.page(dto);
        return Result.success(pageResult);
    }

    @CacheEvict(cacheNames = "setmeal", key = "#setmealDTO.categoryId")
    @PostMapping
    public Result addSetmeal(@RequestBody SetmealDTO dto) {
        setmealService.addSetmeal(dto);
        return Result.success();
    }

    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        setmealService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        SetmealVO vo = setmealService.getById(id);
        return Result.success(vo);
    }

    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @PostMapping("/status/{status}")
    public Result enableOrDisable(@PathVariable Integer status, Long id) {
        setmealService.enableOrDisable(status, id);
        return Result.success();
    }

}